package com.example.demo.controller;

import com.example.demo.domain.Event;
import com.example.demo.result.RespBean;
import com.example.demo.service.EventServiceImpl;
import com.example.demo.webSocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/EventManage")
@CrossOrigin
public class EventController {
    @Autowired
    private EventServiceImpl eventService;

    @PostMapping("/addEvent")
    @ResponseBody
    public RespBean addEvent(@RequestBody Event event){
        Timestamp d = new Timestamp(System.currentTimeMillis());
        event.setEvent_date(d.toString());
        eventService.addEvent(event);
        try {
            WebSocket.BroadCastInfo("111");
        } catch (IOException e) {
            e.printStackTrace();
            return RespBean.error("error");
        }
        return RespBean.success("success");
    }

    @GetMapping(value = "/deleteEvent")
    @ResponseBody
    public RespBean deleteEvent(@RequestParam("event_id") int id){
        eventService.deleteEvent(id);
        return RespBean.success("success");
    }

    @PostMapping(value = "/updateEvent")
    @ResponseBody
    public RespBean updateEvent(@RequestBody Event event){
        eventService.updateEvent(event);
        return RespBean.success("success");
    }

    @GetMapping(value = "/findAll")
    @ResponseBody
    public RespBean findAll(){
        List<Event> events;
        events = eventService.selectAll();
        return RespBean.success("success",events);
    }

    @GetMapping(value = "/findOneEvent")
    @ResponseBody
    public RespBean findOneEvent(@RequestParam("event_id") int id){
        Event event;
        event = eventService.selectEvent(id);
        return RespBean.success("success",event);
    }
}
